package com.yrihr.wisdom.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yrihr.wisdom.entity.News;
import com.yrihr.wisdom.service.NewsSercice;
import com.yrihr.wisdom.util.JsoupUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName : DayJobController
 * @Description : 每天定时爬取
 * @Author : wind
 * @Date: 2020-11-18 08:24
 * @Version 1.0
 */
@Component
@RequiredArgsConstructor
@Api(tags = "每天自动进行爬取,8-23点，每10分钟爬取一次")
@Slf4j
public class DayJobController {

    private final NewsSercice newsSercice;

    /*@Scheduled(cron = "0 0/10 8-23 * * ? ")*/
    @ApiOperation("按照起始日期进行爬取")
    public void job() {
        // 两个日期间循环
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat sdfday = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdfday.format(date);
        // 返回的日期集合
        List<String> dayList = new ArrayList<String>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date start = new Date();
        Date end = start;

        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
        while (tempStart.before(tempEnd)) {
            dayList.add(dateFormat.format(tempStart.getTime()));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        for (String day : dayList) {

        /*http://mwr.ckcest.cn/wnews/wrNewsB/getList?pageNo=1&pageSize=1&newsNm=&proCat=&startDate=2020-11-16&
        endDate=2020-11-16&sqlMap%5Bsql%5D=&sqlMap%5BstartDate%5D=2020-11-16&sqlMap%5BendDate%5D=2020-11-16*/
            Document doc = null;
            Integer pageNo = 1;
            Integer pageSize = 1;
            String url = "http://mwr.ckcest.cn/wnews/wrNewsB/getList?pageNo=" + pageNo
                    + "&pageSize=" + pageSize
                    + "&newsNm=&" + "proCat=&startDate=" + day + "&endDate=" + day
                    + "&sqlMap%5Bsql%5D=&sqlMap%5BstartDate%5D=" + day
                    + "&sqlMap%5BendDate%5D=" + day;
            try {
                doc = JsoupUtil.getConnection(url, "POST", "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            }
            JSONObject body = JSONObject.parseObject(doc.body().text());
            JSONObject list = body.getJSONObject("obj");
            // 每页显示一个，这样pages就是总页数及总个数
            /*{"success":true,"msg":"操作成功","obj":{"isNewRecord":true,"pageNo":1,"pageSize":1,"count":525,"pages":525,"list":*/
            int pages = (int) list.get("pages");
            if (pages > 0) {
                for (int i = 0; i < pages + 1; i++) {
                    try {
                        Thread.sleep(200);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    pageNo = i;
                    url = "http://mwr.ckcest.cn/wnews/wrNewsB/getList?pageNo=" + pageNo
                            + "&pageSize=" + pageSize
                            + "&newsNm=&" + "proCat=&startDate=" + day + "&endDate=" + day
                            + "&sqlMap%5Bsql%5D=&sqlMap%5BstartDate%5D=" + day
                            + "&sqlMap%5BendDate%5D=" + day;
                    try {
                        doc = JsoupUtil.getConnection(url, "POST", "utf-8");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        body = JSONObject.parseObject(doc.body().text());
                        list = body.getJSONObject("obj");
                        List<Object> listNews = (List<Object>) list.get("list");
                        for (int j = 0; j < listNews.size(); j++) {
                            News news = new News();
                            Map map = (Map) listNews.get(j);
                            news.setId(map.get("id") + "");
                            news.setProCat(map.get("proCat") + "");
                            news.setNewsRsn(map.get("newsRsn") + "");
                            news.setNewsCd(map.get("newsCd") + "");
                            news.setId(map.get("id") + "");
                            news.setNewRecord((Boolean) map.get("isNewRecord"));
                            news.setDetailUrl(map.get("detailUrl") + "");
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            news.setPubDate(sdf.parse(map.get("pubDate") + ""));
                            news.setNewsNm(map.get("newsNm") + "");
                            news.setUpdateTime(sdf.parse(sdf.format(new Date())));
                            QueryWrapper<News> query = Wrappers.query();
                            query.eq("newsCd", news.getNewsCd());
                            query.eq("proCat", news.getProCat());
                            int count = newsSercice.count(query);
                            if (count == 0) {
                                newsSercice.save(news);
                            } else {
                                // 判断发布时间是否正确
                                query.ne("pubDate", news.getPubDate());
                                count = newsSercice.count(query);
                                // 如果找到id=id  其他不同，即count=1，找到了不同，则需要更新
                                // 如果找到id=id  其他全相同，即结果count=0,没有找到不同，则不更新
                                // 这两句话太绕了
                                if (count == 1) {
                                    boolean b = newsSercice.saveOrUpdate(news);
                                    log.info("存在：1条记录，id为：" + news.getNewsCd() + "但发布时间与网站不匹配，则需更新，即将更新！");
                                    if (b) {
                                        log.info("存在：1条记录，id为：" + news.getNewsCd() + "但发布时间与网站不匹配，已更新成功！");
                                    }
                                } else {
                                    log.info("存在：1条记录，id为：" + news.getNewsCd() + "发布时间与网站匹配，无需更新");
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        continue;
                    }
                }
            }
        }
    }
}
